<template>
  <div v-if="visible" class="modal-overlay" @click="closeModal">
    <div class="modal-container" @click.stop>
      <div class="modal-header">
        <h3>用户登录</h3>
        <button class="close-btn" @click="closeModal">
          <i class="fas fa-times"></i>
        </button>
      </div>
      
      <div class="modal-body">
        <form @submit.prevent="handleLogin">
          <div class="form-group">
            <label for="username">用户名</label>
            <input
              id="username"
              type="text"
              class="form-control"
              placeholder="请输入用户名"
              v-model="loginForm.username"
              required
            >
          </div>
          
          <div class="form-group">
            <label for="password">密码</label>
            <input
              id="password"
              type="password"
              class="form-control"
              placeholder="请输入密码"
              v-model="loginForm.password"
              required
            >
          </div>
          
          <div class="form-actions">
            <button type="button" class="btn btn-secondary" @click="closeModal">
              取消
            </button>
            <button type="submit" class="btn btn-primary" :disabled="loading">
              <i v-if="loading" class="fas fa-spinner fa-spin"></i>
              {{ loading ? '登录中...' : '登录' }}
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { login, setToken } from '../api'
import { setLoginStatus } from '../stores/auth'

// 定义组件属性
interface Props {
  visible: boolean
}

// 定义组件事件
interface Emits {
  (e: 'update:visible', value: boolean): void
  (e: 'login', data: { username: string; password: string }): void
}

defineProps<Props>()
const emit = defineEmits<Emits>()

// 响应式数据
const loading = ref(false)
const loginForm = reactive({
  username: '',
  password: ''
})

// 关闭弹窗
const closeModal = () => {
  emit('update:visible', false)
  // 重置表单
  loginForm.username = ''
  loginForm.password = ''
  loading.value = false
}

// 处理登录
const handleLogin = async () => {
  if (!loginForm.username.trim() || !loginForm.password.trim()) {
    alert('请输入用户名和密码')
    return
  }
  
  loading.value = true
  
  try {
    // 调用登录接口
    const response = await login({
      xh: loginForm.username,
      password: loginForm.password,
      authMethod: 'unified'
    })
    
    // 保存token
    setToken(response.data.token)
    
    // 设置登录状态
    setLoginStatus(true, response.data.user)
    
    // 触发登录事件，传递用户信息
    emit('login', {
      username: loginForm.username,
      password: loginForm.password
    })
    
    // 登录成功后关闭弹窗
    closeModal()
    alert(`登录成功！欢迎 ${response.data.user.name}`)
    
  } catch (error: any) {
    console.error('登录失败:', error)
    alert(error.message || '登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-container {
  background-color: #34495e;
  border-radius: 15px;
  width: 90%;
  max-width: 400px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  overflow: hidden;
}

.modal-header {
  background-color: #2c3e50;
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #1a252f;
}

.modal-header h3 {
  margin: 0;
  color: white;
  font-size: 1.5rem;
}

.close-btn {
  background: none;
  border: none;
  color: white;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 5px;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.3s ease;
}

.close-btn:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.modal-body {
  padding: 30px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: white;
  font-weight: 500;
}

.form-control {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #4a6572;
  border-radius: var(--border-radius);
  font-size: 1rem;
  background-color: #4a6572;
  color: white;
  transition: border-color 0.3s ease;
}

.form-control:focus {
  outline: none;
  border-color: var(--primary);
}

.form-control::placeholder {
  color: #bdc3c7;
}

.form-actions {
  display: flex;
  gap: 15px;
  justify-content: flex-end;
  margin-top: 30px;
}

.btn {
  padding: 12px 24px;
  border: none;
  border-radius: var(--border-radius);
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background-color: var(--primary);
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background-color: #2980b9;
}

.btn-secondary {
  background-color: #6c757d;
  color: white;
}

.btn-secondary:hover {
  background-color: #5a6268;
}

.fa-spinner {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
</style>
